package com.shanghaionstar.user;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.onstar.cn.common.exception.HttpException;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.more.entity.SubscriberDTO;
import com.onstar.cn.weight.DeleteEditText;
import com.shanghaionstar.R;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.user.iview.IPayCardOtherActiveView;
import com.shanghaionstar.user.presenter.PayCardOthetActivePresenter;
import com.shanghaionstar.utils.AlertDialogUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.weight.TopLayout;

import java.util.Locale;

/**
 * Created by Mengzhou Gu on 2016/8/3.
 */
public class PayCardOtherActiveFragment extends BaseFragment implements IPayCardOtherActiveView,View.OnClickListener{
    @FindViewById
    private TopLayout top_layout = null;
    private PrePayActivity parentActivity;
    @FindViewById
    private DeleteEditText et_car_recognition_code = null;
    private ProgressDialog dialog;
    private SubscriberDTO subscriberDTO = null;
    @FindViewById
    private Button next_btn;

    private PayCardOthetActivePresenter presenter;
    @Override
    public int getLayoutId() {
        return R.layout.user_paycard_othetactive_fragment;
    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        parentActivity = (PrePayActivity) getActivity();
        parentActivity.current_fragment = "PayCardOtherActiveFragment";
        presenter=new PayCardOthetActivePresenter(parentActivity.getScheduler(),parentActivity,et_car_recognition_code);
        presenter.setView(this);
        et_car_recognition_code.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus) {
                    BusinessUtil.sendReportByClickType(getActivity(),FunctionNameFindId.Activeforothers_VIN.getStringValue());
                }
            }
        });

      top_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                CommonUtil.hideKeyBoard(getActivity(), et_car_recognition_code);
                BusinessUtil.sendReportByClickType(getActivity(),FunctionNameFindId.Activeforothers_back.getStringValue());
                back();
            }
        });

    }


//    public void changeFragment(FragmentManager fragmentManager, Fragment fragment, String tag) {
//        if (null != fragmentManager.findFragmentByTag(tag))
//            return;
//        FragmentTransaction p_FragmentTransaction = fragmentManager.beginTransaction();
//        p_FragmentTransaction.replace(R.id.fragment_container, fragment, tag);
//        p_FragmentTransaction.addToBackStack(null);
//        p_FragmentTransaction.commitAllowingStateLoss();
//    }


    @OnClick({R.id.next_btn})
    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.next_btn:
                BusinessUtil.sendReportByClickType(getActivity(), FunctionNameFindId.PreSaleForOtherPersonVIN.getStringValue());
                CommonUtil.hideKeyBoard(getActivity(), et_car_recognition_code);
                if (CommonUtil.isNull(et_car_recognition_code.getText().toString())) {
                    // 不能为空
                    AlertDialogUtil.showAlertDialog(getActivity(), R.string.dialog_warning,
                            R.string.input_user_info_not_null);
                    return;
                }
                if (!CommonUtil.isVin(et_car_recognition_code.getText().toString())) {
                    // 格式错误
                    AlertDialogUtil.showAlertDialog(getActivity(), R.string.dialog_warning,
                            R.string.SB027_MSG003);
                    return;
                }

              //  getAccountInfoByVin();
                presenter.getData();
                break;
        }
    }


    @Override
    public void showSuccess(SubscriberDTO subscriberDTO) {
        Log.d("subscriberDTO",subscriberDTO.toString());
        if (subscriberDTO != null) {
            Bundle bundle = new Bundle();
            bundle.putSerializable("account_obj", subscriberDTO);
            bundle.putString("vin", et_car_recognition_code.getText().toString().toUpperCase(Locale.CHINA).trim());
            UsePrepayCardFragment userprepay = new UsePrepayCardFragment();
            userprepay.setArguments(bundle);
//            parentActivity.changeFragment(parentActivity.fragmentManager, carRecognifragment, PayCardOtherActiveFragment.class, true);
            parentActivity.changeFragment(parentActivity.fragmentManager, userprepay, UsePrepayCardFragment.class,true);
        }
    }

    @Override
    public void showFailure() {

    }

    @Override
    public void showError(Throwable throwable) {
        AlertDialogUtil.showDialogByExceptionCode(getActivity(), (HttpException) throwable);
    }

    @Override
    public void setLoading(boolean isLoading) {
        if(isLoading){
            dialog=showLoadProgress();
            dialog.show();
        }else{
            dismissDialog(dialog);
        }
    }

    @Override
    public void onResponseError(OnStarException e) {

    }

    @Override
    public void onReponseSuccess(SubscriberDTO object) {

    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }

    @Override
    public void onFragmentBackPress() {
//        back();
        parentActivity.getSupportFragmentManager().popBackStack();
        BusinessUtil.sendReportByClickType(getActivity(),FunctionNameFindId.Activeforothers_back.getStringValue());

    }
}
